package org.openrbac.rbac.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.openrbac.rbac.entity.Auth;
import org.openrbac.rbac.entity.Role;
import org.openrbac.rbac.entity.User;

import java.util.List;

/**
 * @author bapie
 * @description 针对表【user(系统用户)】的数据库操作Mapper
 * @createDate 2024-04-03 15:37:15
 * @Entity org.openrbac.entity.User
 */
public interface UserMapper extends BaseMapper<User> {

    @Select("select id,nickname,gender,email,avatar_path from user where username=#{username}")
    User getUser(@Param("username") String username);

    /**
     * 查询所有用户及其所有信息
     *
     * @return 用户列表
     */
    @Select("select * from user")
    List<User> getAllUser();

    @Select("select id,username,password,nickname,gender,phone,email,avatar_path,status from user")
    List<User> getAllUserPartMessage();
    /**
     * 根据用户名查询用户ID
     *
     * @param username 用户名
     * @return 用户ID
     */
    @Select("select id from user where username=#{username}")
    Long selectIdByUsername(@Param("username") String username);

    /**
     * 根据用户ID查询角色
     *
     * @param userId 用户ID
     * @return 角色
     */
    @Select("select id, rolename from user_role left join role on user_role.role_id = role.id where user_id=#{userId}")
    List<Role> listRolesByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID查询角色ID
     *
     * @param userId 用户ID
     * @return 角色ID
     */
    @Select("select id from user_role left join role on user_role.role_id = role.id where user_id=#{userId}")
    List<Long> listRoleIdsByUserId(@Param("userId") Long userId);

    /**
     * 根据角色ID查询权限
     *
     * @param roleIds 角色ID
     * @return 权限
     */
    @Select("<script>" +
            "select distinct auth.* from role_auth left join auth on role_auth.auth_id=auth.id where role_auth.role_id in " +
            "<foreach collection='roleIds' item='item' open='(' separator=',' close=')'>" +
            "   #{item}" +
            "</foreach>" +
            "</script>")
    List<Auth> listAuthByRoleIds(@Param("roleIds") List<Long> roleIds);

    /**
     * 根据用户ID删除角色
     *
     * @param userId 用户ID
     * @return boolean
     */
    @Delete("delete from user_role where user_id=#{userId}")
    boolean deleteRolesByUserId(@Param("userId") Long userId);

    /**
     * 批量插入用户ID和角色ID
     *
     * @param userId  用户ID
     * @param roleIds 角色ID
     * @return boolean
     */
    @Insert("<script>" +
            "insert user_role(user_id, role_id) values " +
            "<foreach collection='roleIds' item='item' separator=','>" +
            "   (#{userId},#{item})" +
            "</foreach>" +
            "</script>")
    boolean insertRolesBatch(@Param("userId") Long userId, @Param("roleIds") List<Long> roleIds);
}




